package com.easyhuan.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.easyhuan.pojo.Goods;
import com.easyhuan.pojo.PageInfo;
import com.easyhuan.pojo.Picture;
import com.easyhuan.pojo.User;
import com.easyhuan.util.HibernateUtil;

public class PictureUtil {

	// 删除单个商品
	public boolean deletePicture(Session session, Transaction trans,
			Picture picture) {
		try {
			trans.begin();
			session.delete(picture);
			trans.commit();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
	}

	// 删除多个商品
	public boolean deletePictureList(Session session, Transaction trans,
			List<Picture> list) {
		try {
			trans.begin();
			for (Picture picture : list) {
				session.delete(picture);
			}
			trans.commit();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}

	// 修改商品
	public static boolean updatePojo(Session session, Transaction trans,
			Picture picture) {
		try {
			trans.begin();
			session.update(picture);
			trans.commit();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
	}

	// 根据Id查询商品信息
	public Picture selectGoodsById(Session session, String id) {
		String hql = "FROM Picture WHERE pictureId = ?";
		Integer Id = Integer.parseInt(id);
		Query query = session.createQuery(hql);
		query.setInteger(0, Id);
		Picture picture = (Picture) query.uniqueResult();
		return picture;
	}

	public String getHQL(String pictureItem, String condition) {
		String hql = "FROM Picture WHERE " + pictureItem + " like '%"
				+ condition + "%'";
		return hql;
	}

	// 获取在特定条件下的记录条数
	public Long getResultCount(String tableName) throws Exception {
		Session session = HibernateUtil.getHibernateSession();
		Long resultCount = (Long) session.createQuery(
				"SELECT count(p) FROM " + tableName + " p").uniqueResult();
		session.close();
		return resultCount;
	}
}
